package com.bawei.park2211a.visit

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.base.bean.VisitEntity
import com.bawei.base.mvvm.BaseActivity
import com.bawei.park2211a.adapter.VisitAdapter
import com.bawei.park2211a.adapter.VisitTypeAdapter
import com.bawei.park2211a.databinding.ActivityVisitBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/visit/VisitActivity")
class VisitActivity : BaseActivity<ActivityVisitBinding, VisitViewModel>() {
    private lateinit var visitTypeAdapter: VisitTypeAdapter
    private lateinit var visitAdapter: VisitAdapter
    private var visitList = mutableListOf<VisitEntity>()
    private var index = 0

    override fun initView() {
        viewModel.visit()
        visitAdapter = VisitAdapter(viewModel.apiService, this)
        visitTypeAdapter = VisitTypeAdapter()
        var visitTitles = mutableListOf<String>()
        visitTitles.add("全部状态")
        visitTitles.add("待审核")
        visitTitles.add("已通过")
        visitTitles.add("已拒绝")
        visitTitles.add("已完成")
        visitTypeAdapter.data.addAll(visitTitles)
        binding.visitTypeRec.adapter = visitTypeAdapter
        binding.visitTypeRec.layoutManager =
            LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        binding.visitRec.adapter = visitAdapter
        binding.visitRec.layoutManager = LinearLayoutManager(this)
        visitTypeAdapter.setOnItemClickListener { _, _, position ->
            visitTypeAdapter.setTypeId(visitTypeAdapter.data[position], index, position)
            index = position
            visitAdapter.data.clear()
            when (position) {
                0 -> {
                    visitAdapter.data.addAll(visitList)
                }
                1 -> {
                    visitList.forEach {
                        if (it.visitStatus == "待审核") {
                            visitAdapter.data.add(it)
                        }
                    }
                }
                2 -> {
                    visitList.forEach {
                        if (it.visitStatus == "已通过") {
                            visitAdapter.data.add(it)
                        }
                    }
                }
                3 -> {
                    visitList.forEach {
                        if (it.visitStatus == "驳回") {
                            visitAdapter.data.add(it)
                        }
                    }
                }
                4 -> {
                    visitList.forEach {
                        if (it.visitStatus == "已完成") {
                            visitAdapter.data.add(it)
                        }
                    }
                }
            }
            visitAdapter.notifyDataSetChanged()
        }
        binding.addVisit.setOnClickListener {
            ARouter.getInstance().build("/app/visit/AddVisitActivity").navigation()
        }
    }

    override fun initData() {
        viewModel.visitList.observe(this) {
            visitList.clear()
            visitList.addAll(it)
            visitAdapter.data.clear()
            visitAdapter.data.addAll(it)
            for (i in 0 until it.size) {
                visitAdapter.notifyItemChanged(i)
            }

        }
    }
}